package classification;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import utilities.DCUtilities;

import model.Category;
import model.Document;
import model.Neighbour;

public class Classificator {
	
	public static void assignClass(Document doc, List<Neighbour> neighbours){
		HashMap<String, Category> categories = new HashMap<String, Category>();
		
		for (Neighbour n:neighbours){
			for (Category c:n.getNeighbur().getCategories().values()){
				Category temp = categories.get(c.getCategoryName());
				if (temp == null){
					c.setTempWeight(0.0);
					categories.put(c.getCategoryName(), c);
				} else {
					temp.increaseTempWeight(c.getTempWeight());
				}
			}
		}
		List<Category> categoriesList = new ArrayList<Category>(categories.values());
		Collections.sort(categoriesList, new CategoryComparator());
		
		HashMap<String, Category> newCategories = new HashMap<String, Category>();
		int newCategoriesCount = DCUtilities.NEW_CATEGORIES_COUNT;
		if (newCategoriesCount > categoriesList.size()){
			newCategoriesCount = categoriesList.size();
		}
		for (int i=0; i<newCategoriesCount; i++){
			Category newCategory = categoriesList.get(i);
			newCategories.put(newCategory.getCategoryName(), newCategory);
		}
		doc.setNewCategories(newCategories);
	}
	
}
